<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *页面管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$_SESSION['plugin_name'] = 'jy_meiye';
if($_SESSION['plugin_name'] && $_SESSION['plugin_name'] !='core'){
	if (method_exists($this->plugin($_SESSION['plugin_name']),'getplugins')){
		$getplugins = $this->plugin($_SESSION['plugin_name'])->getplugins();
	}
}

$plugins =array();
if($getplugins){
	foreach($getplugins as &$type){
			foreach($type['list'] as $k=>&$p){
				if($p['is_install']){
					$plugins[]=$p;
				}else{
					unset($type['list'][$k]);
				}
			}
	}
}
/*if(empty($_SESSION['actiontypes'])){
	$types =array();
	$plugins = pdo_getall('core_plugins',array(),array('name','title'));
	foreach($plugins as $p){
		$isdiy = pdo_getall('core_mobile_blocks',array('type'=>1,'plugin_id'=>$p['name']));
		$install = check_permission($p['name']);
		if(!empty($isdiy) && $install){
			$actiontypes = $this->plugin($p['name'])->getactiontypes();
				foreach($actiontypes as $u=>$t){
					$types[$p['name'].'/'.$u]=$p['title'].'-'.$t;
				}
		}
	}
	$_SESSION['actiontypes'] = $types;
}*/


if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_SESSION['plugin_name']){
		$params['plugin_name']=$_SESSION['plugin_name'];
	}
	if($_RQ['pageaction']){
		$params['pageaction']=trim($_RQ['pageaction']);
		$return['params']['pageaction']=$_RQ['pageaction'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['plugin_name'])){
		$params['plugin_name']=trim($_RQ['plugin_name']);
		$return['params']['plugin_name']=$_RQ['plugin_name'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','title','createtime','status','html','pageaction','plugin_name');
	$list=pdo_getslice('core_mobile_web',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	if($list){
		foreach($list as &$item){
			$item['preview']=custom_url('core/welcome//id='.$item['id'],true);
			if($_SESSION['plugin_name']){
				$item['url']=custom_url($item['plugin_name'].'/'.$item['pageaction'].'//',true);
				$plugininfo=pdo_get('core_plugins',array('name'=>$item['plugin_name']),array('title','diypage'));
				$item['plugin_title'] = $plugininfo['title'];
				$item['pageaction'] = $item['pageaction'] ? $item['pageaction'] :'无';
				$diypage = json_decode($plugininfo['diypage'],true);
				if($diypage){
					foreach($diypage as $diy){
						if($diy['name']==$item['pageaction']){
							$item['pageaction'] = $diy['title'];
						}
					}
				}
			}else{
				$item['url']=custom_url('core/welcome//id='.$item['id'],true);
			}
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'页面列表'),
			array('action'=>'diypage_template','title'=>'添加页面'),
			array('action'=>'diypage_template','op'=>'my','title'=>'我的模板'),
		),
		'list'=>array('data'=>'list','tip'=>'【温馨提示】:一个页面,只在针对的页面有效；如果启用了就会替换默认的页面！','list'=>array(
			array('name'=>'title','title'=>'页面名称','type'=>'string'),
			array('name'=>'plugin_title','title'=>'所属插件','type'=>'string'),
			array('name'=>'pageaction','title'=>'页面类型','type'=>'string'),
			array('name'=>'status','title'=>'启用/关闭','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
			array('name'=>'preview','title'=>'预览二维码','type'=>'ewm'),
			array('name'=>'preview','title'=>'预览链接','type'=>'copy'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('op'=>'design','title'=>'设计','type'=>'href'),
			array('op'=>'release','title'=>'保存到我的模版','type'=>'href'),
		)),
		'search_one'=>array('select_name'=>'plugin_title','options'=>'plugins','name'=>'plugin_name','add'=>false,'select_title'=>'插件名'),
	);
	$return['plugins'] =$plugins ;
	$return['htmls']=$htmls;
	set_html('header','common/header');
	set_html('title','页面装修');
	//set_html('framess',$this->menu($_SESSION['plugin_name']));
	exi($return);
}
if($_RQ['op']=='edit'){
	//编辑页面，添加页面在页面模版列表那边选择模版然后添加
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择页面！','error');
	}
	$item=pdo_get('core_mobile_web',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title','status','domain','foot','wxapp_id','menu_id','pageaction','plugin_name'));
	if($item['wxapp_id']){
		$item['wxapp_type']=1;
	}else{
		$item['wxapp_type']=3;
	}
	$params=array('id'=>$id);
	if($_SESSION['plugin_name']){
		$params['plugin']=$_SESSION['plugin_name'];
		$params['action']=$item['pageaction'];
	}else{
		$params['plugin']='core';
		$params['action']='welcome';
	}
	$back=cfc('core')->get_back_newss($params);
	$item=array_merge($back,$item);
	$return['item']=$item;
	
	$default=array(
		
	);
	$return['wxapp_types']=array(
		'1'=>'关联已有小程序',
		'2'=>'创建同名小程序并关联',
		'3'=>'不关联小程序',
	);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'设置页面信息',
		'tabs'=>array(
			array('op'=>'list','title'=>'页面列表'),
		),
		'edit'=>array(
			array('type'=>'string','name'=>'title','title'=>'页面名称'),
			cfc('core')->keywords(),
			array('type'=>'select','name'=>'plugin_name','title'=>'选择页面插件','options'=>'plugins','option_value'=>'name'),
			array('type'=>'select','name'=>'pageaction','title'=>'选择页面类型','options'=>'plugins[pagedata.item.plugin_name].pages','option_value'=>'action'),
			array('type'=>'image','name'=>'picurl','title'=>'封面图片'),
			array('type'=>'text','name'=>'description','title'=>'页面描述'),
			array('type'=>'string','name'=>'domain','title'=>'绑定域名'),
		),
	);
	//获取子插件可DIY页面
	if($_SESSION['plugin_name'] && $_SESSION['plugin_name'] !='core'){
		$getplugins = $this->plugin($_SESSION['plugin_name'])->getplugins();
	}
	$diypages=array();
	$plugins[$_SESSION['plugin_name']] = pdo_get('core_plugins',array('name'=>$_SESSION['plugin_name']),array('name','title','diypage'));
	$plugins[$_SESSION['plugin_name']]['diypage'] = $plugins[$_SESSION['plugin_name']]['diypage'] ? $plugins[$_SESSION['plugin_name']]['diypage'] : '[{"action":"index/","title":"首页"}]';
	$plugins[$_SESSION['plugin_name']]['pages']=json_decode($plugins[$_SESSION['plugin_name']]['diypage'],true);
	foreach($getplugins as &$type){
		foreach($type['list'] as $k=>&$p){
			$diypage = pdo_getcolumn('core_plugins',array('name'=>$p['name']),'diypage');
			$diypage = $diypage ? $diypage : '[{"action":"index/","title":"首页"}]';
			if($p['is_install'] && $diypage){
				$plugins[$p['name']]=array(
					'name'=>$p['name'],
					'title'=>$p['title'],
					'pages'=>json_decode($diypage,true),
				);
			}else{
				unset($type['list'][$k]);
			}
		}
	}
	$return['plugins']=$plugins ? $plugins : array();
	$return['htmls']=$htmls;
	$return ['menus']=pdo_getall('core_diy_menu',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['keytypes']=get_keyword_types();
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$id=intval($post['id']);
	if(!$id){
		exi('请先选择页面！','error');
	}
	if(!$post['title']){
		exi('请填写页面标题','error');
	}
	if(!$post['pageaction'] && $_SESSION['plugin_name']){
		exi('请先选择页面类型！','error');
	}
	//插入回复规则
	$data=array_fields(array('title','status','description','picurl','keywords'),$post);
	$data['plugin']=$_SESSION['plugin_name']?$_SESSION['plugin_name']:'core';
	$data['action']=$_SESSION['plugin_name']?$post['pageaction']:'welcome';
	$data['id']=$id;
	cfc('core')->save_back_news($data);
	//更新页面数据
	$data=array_fields(array('title','status','foot','domain','menu_id'),$post);
	$data['plugin_name']=$_SESSION['plugin_name']?$_SESSION['plugin_name']:'core';
	//$post['pageaction'] = explode('/',$post['pageaction']);
	$data['pageaction']=$_SESSION['plugin_name']?$post['pageaction']:'welcome';
	if($post['wxapp_type']==1 && $post['wxapp_id']){
		$data['wxapp_id']=intval($post['wxapp_id']);
	}
	if($post['wxapp_type']==3){
		$data['wxapp_id']=0;
	}
	if($post['wxapp_type']==2){
		//创建同名小程序并关联，先检查是否存在关联小程序  先检查小程序数量限制
		if($_SESSION['account']['groupid']){
			$limit=pdo_getcolumn('core_account_group',array('id'=>$_SESSION['account']['groupid']),'wxapp_num');
			$num=pdo_count('core_account_wxapp',array('uniacid'=>$_SESSION['uniacid']));
			if($num>=$limit){
				exi('您所添加的小程序已达到最大值，如需创建更多，请联系管理员提升权限或者升级套餐！','error');
			}
		}
		
		$check=pdo_getcolumn('core_account_wxapp',array('web_id'=>$id),'id');
		if($check){
			exi('该页面已关联id为“'.$check.'”的小程序，不能同时关联多个小程序！','error');
		}
		$insert=pdo_get('core_mobile_web',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('items','setting'));
		$insert['uniacid']=$_SESSION['uniacid'];
		$insert['type']=1;
		$insert['web_id']=$id;
		$insert['title']=$data['title'];
		pdo_insert('core_account_wxapp',$insert);
		$data['wxapp_id']=pdo_insertid();
	}
	pdo_update('core_mobile_web',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('保存成功！','','list');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的页面！','error');
	}
	$post=get_postdata();
	$status =intval($post['status']);
	if(isset($post['status'])){
		$pageinfo = pdo_get('core_mobile_web',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),array('id','pageaction'));
		$isstatus = pdo_getall('core_mobile_web',array('uniacid'=>$_SESSION['uniacid'],'status'=>1,'plugin_name'=>$_SESSION['plugin_name'],'pageaction'=>$pageinfo['pageaction']),array('id','title'));
		if(!empty($isstatus) && $status == 1){
			exi('同种类型页面,只能开启一个模板','error');
		}else{
			pdo_update('core_mobile_web',array('status'=>$status),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('操作成功！');
}
if($_RQ['op']=='release'){
	//发布到模版市场，发布到模版市场是否需要设置价格和分类？暂时先不发布，直接保存为我的模版
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['web_id']);
		if(!$id){
			exi('请先选择页面！','error');
		}
		$data=pdo_get('core_mobile_web',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('items','setting','pageaction','plugin_name'));
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'type'=>intval($post['type']),
			'title'=>$post['title'],
			'image'=>$post['image'],
			'items'=>$data['items'],
			'setting'=>$data['setting'],
			'pageaction'=>$data['pageaction'],
			'plugin_name'=>$data['plugin_name']
		);
		pdo_insert('core_mobile_template',$insert);
		exi('保存模版成功！','','list');
	}else{
		$id=intval($_RQ['id']);
		if(!$id){
			exi('请先选择要保存的页面！','error');
		}
		$return['item']=array('web_id'=>$id);
		$return['types']=pdo_getall('core_template_group',array(),array('id','title'));
		$return['htmls']=array(
			'type'=>'edit',
			'desc'=>'设置模版信息',
			'edit'=>array(
				array('type'=>'string','name'=>'title','title'=>'模版名称'),
				array('type'=>'select','name'=>'type','title'=>'模版类型','options'=>'types'),
				array('type'=>'image','name'=>'image','title'=>'模版图片','help'=>'上传一张图片展示模版效果'),
			),
		);
		exi($return);
	}
}
if($_RQ['op']=='design'){
	//页面设计
	if($_RQ['postdata']){
		$post=get_postdata();
		//exi('cs','error');
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择页面！','error');
		}
		$update=array(
			'items'=>$post['items']?ijson_encode($post['items']):'',
			'setting'=>$post['setting']?ijson_encode($post['setting']):''
		);
		if($update['items'] || $update['setting']){
			//检测恶意代码
			if(strexists($update['items'],'<?') || strexists($update['setting'],'<?')){
				exi('提交内容存在恶意代码，无法提交！','error');
			}
			if($post['type']!='template'){//type==template表示设计的是模版，设计模版时无需生成具体页面
				//获取开发者自己的组件，并上传
				$local_blocks=pdo_getall('core_mobile_blocks',array('madehtml <>'=>''),array('cloud_id','default','madehtml'),'cloud_id');
				if($local_blocks){
					foreach($local_blocks as &$row){
						$row['madehtml']=cfc('files')->source_read($row['madehtml']);
						$row['default']=$row['default']?json_decode($row['default'],true):array();
					}
				}
				//设计完成后，对接云端生成页面
				$post=array('items'=>base64_encode($update['items']),'setting'=>base64_encode($update['setting']),'local_blocks'=>$local_blocks);
				$html=cloud_request('diy.mobile.made',$post);
				//将获得生成的html代码存入文件
				$item=pdo_get('core_mobile_web',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('html'));
				$html=cfc('files')->source_save($html,'page','html','页面页面：'.$id,$item['html']);
				if(!$item['html']){
					$update['html']=$html;
				}
			}
		}
		if($post['type']=='template'){
			pdo_update('core_mobile_template',$update,array('id'=>$id));
			exi('保存成功！','','goback');
		}
		$update['updatetime']=TIMESTAMP;
		pdo_update('core_mobile_web',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$wxapp_id=pdo_getcolumn('core_mobile_web',array('id'=>$id),'wxapp_id');
		if($wxapp_id){
			$data=array(
				'items'=>$update['items'],
				'setting'=>$update['setting'],
			);
			pdo_update('core_account_wxapp',$data,array('id'=>$wxapp_id,'uniacid'=>$_SESSION['uniacid']));
		}
		$aliapp_id=pdo_getcolumn('core_account_aliapp',array('web_id'=>$id),'id');
		if($aliapp_id){
			$data=array(
				'items'=>$update['items'],
				'setting'=>$update['setting'],
			);
			pdo_update('core_account_aliapp',$data,array('id'=>$aliapp_id,'uniacid'=>$_SESSION['uniacid']));
		}
		exi('保存成功！','','list');
	}else{
		$id=intval($_RQ['id']);
		$art_num=intval($_RQ['art_num']);
		$fenlei_num=intval($_RQ['fenlei_num']);
		$store_num=intval($_RQ['store_num']);
		if(!$id){
			exi('请先选择要设计的页面！','error');
		}
		
		$return['used_navs']=array();
		if($_RQ['type']=='template'){
			$item=pdo_get('core_mobile_template',array('id'=>$id),array('id','title','items','setting'));
			$item['type']='template';
		}else{
			$item=pdo_get('core_mobile_web',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','title','items','setting','group_id'));
			$_SESSION['mobile_web']['group_id']=$item['group_id'];
		}
		$item['items']=$item['items']?json_decode($item['items'],true):array();
		if($item['items']){
			foreach($item['items'] as $row){
				if(!$return['used_navs'][$row['nid']]){
					$return['used_navs'][$row['nid']]=pdo_get('core_mobile_blocks',array('cloud_id'=>$row['nid']),array('name','showhtml','edithtml'));
				}
			}
		}
		$item['setting']=$item['setting']?json_decode($item['setting'],true):array('guide'=>0);
		$navs0=pdo_getall('core_mobile_blocks',array('type'=>0,'basictype'=>0,'support <>'=>2),array(),'','usenum desc',array('10'));
		if($navs0){
			foreach($navs0 as &$nav){
				$nav['default']=json_decode($nav['default'],true);
			}
		}
		$navs1=pdo_getall('core_mobile_blocks',array('type'=>0,'basictype'=>1,'support <>'=>2),array(),'','usenum desc',array('10'));
		if($navs1){
			foreach($navs1 as &$nav){
				$nav['default']=json_decode($nav['default'],true);
			}
		}
		// 应用组件
		$pluginparams = array('type'=>1,'support <>'=>2);
		$plugins = $this->plugins($_SESSION['plugin_name']);
		if(!empty($plugins)){
			$pluginparams['plugin_id']=$plugins;
			$navs2=pdo_getall('core_mobile_blocks',$pluginparams,array(),'','usenum desc',array('10'));
			if($navs2){
				$plugintitles=array();
				$plugins=array();
				$i = 0;
				foreach($navs2 as $k=>&$nav){
					$plugintitle=pdo_getcolumn('core_plugins',array('name'=>$nav['plugin_id']),'title');
					$nav['default']=json_decode($nav['default'],true);
					if (in_array($plugintitle,$plugintitles)){
						
					}else{
						$plugintitles[$i] = $plugintitle;
						$plugins[$i]['name'] = $nav['plugin_id'];
						$plugins[$i]['plugintitle']=$plugintitle;
						$blocks = pdo_getall('core_mobile_blocks',array('type'=>1,'plugin_id'=>$nav['plugin_id']),array(),'','usenum desc',array('10'));
						foreach($blocks as &$b){
							$b['default']=json_decode($b['default'],true);
						}
						$plugins[$i]['navs'] = $blocks;
						$i  += 1;
					}
					
				}
			}
		}
		
		$return['navs0']=$navs0;
		$return['navs1']=$navs1;
		$return['navs2']=$plugins;
		$return['edit']=3;
		$item['setting']['top']['title']=$item['setting']['top']['title']?$item['setting']['top']['title']:$item['title'];
		$return['menus']=pdo_getcolumn('core_diy_menu',array('uniacid'=>$_SESSION['uniacid'],'plugin'=>'core','status'=>1),'data');
		$return['menus']=json_decode($return['menus'],true);
		$item['setting']['footer']['menus']=!empty($item['setting']['footer']['menus']) ? $item['setting']['footer']['menus']:$return['menus']['menus'];
		$return['fenleis']=pdo_getall('core_account_article_fenlei',array('uniacid'=>$_SESSION['uniacid']));
		$return['articles']=pdo_getall('core_account_article',array('uniacid'=>$_SESSION['uniacid']));
		$return['store_group']=pdo_getall('core_account_store_group',array('uniacid'=>$_SESSION['uniacid']));
		$return['article_store']=pdo_getall('core_account_store',array('uniacid'=>$_SESSION['uniacid']));
		$return['account']=$_SESSION['account'];
		$return['item']=$item;
		$return['extradata']=array();
		exi($return);
	}
}
if($_RQ['op']=='link'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_SESSION['plugin_name']){
		$params['plugin_name']=$_SESSION['plugin_name'];
	}
	$list=pdo_getall('core_mobile_web',$params,array('id','title'));
	if($list){
		foreach($list as &$item){
			$item['title']=$item['title']?$item['title']:'未命名';
			$item['url']='core/welcome/main/id='.$item['id'];
		}
	}
	if($_RQ['from']=='action'){
		$query='from=action';
	}else{
		$query='';
	}
	$return['list']=$list;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'link','title'=>'本应用官网页'),
			array('plugin'=>$_SESSION['plugin_name']?$_SESSION['plugin_name']:'jy_weishop','action'=>'link','title'=>'本应用功能页'),
			array('plugin'=>'core','action'=>'tools.link','query'=>$query,'title'=>'系统其他页面'),
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'pagelist'=>false,'list'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string'),
		),'action'=>array(
			array('type'=>'modal_result','name'=>'url','title'=>'选取'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	exi($return);
}
if($_RQ['op']=='getnav'){
	$params=array();
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
	}
	$navs=pdo_getall('core_mobile_blocks',$params,array(),'','usenum desc',array('10'));
	exi($navs);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_mobile_web',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('core_mobile_web',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
